public class MySingleList implements IList {

    //节点的内部类
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    //头结点head属于链表的成员
    public ListNode head;

    public void CreateList() {
        ListNode node1 = new ListNode(11);
        ListNode node2 = new ListNode(12);
        ListNode node3 = new ListNode(13);
        ListNode node4 = new ListNode(14);
        ListNode node5 = new ListNode(15);
        ListNode node6 = new ListNode(16);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;

        this.head = node1;

    }

    @Override
    public void addFirst(int data) {
        ListNode NewNode = new ListNode(data);
        if (this.head == null) {
            this.head = NewNode;
            return;
        }
        NewNode.next = this.head;
        this.head = NewNode;
    }

    @Override
    public void addLast(int data) {
        ListNode NewNode = new ListNode(data);
        ListNode CurNode = this.head;
        if (this.head == null) {
            head = NewNode;
            return;
        }
        while (CurNode.next != null) {
            CurNode = CurNode.next;
        }
        CurNode.next = NewNode;
    }

    @Override
    public void addIndex(int index, int data) throws Poslllegal {
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        if (index < 0 || index > size()) {
            System.out.println("插入位置不合法");
            throw new Poslllegal("插入下标元素异常:" + index);
        }
        ListNode NewNode = new ListNode(data);
        ListNode CurNode = searchPrevNode(index);
        NewNode.next = CurNode.next;
        CurNode.next = NewNode;
    }

    private ListNode searchPrevNode(int index) {
        ListNode CurNode = this.head;
        int count = 0;
        while (count != index - 1) {
            CurNode = CurNode.next;
        }
        return CurNode;
    }

    @Override
    public boolean contains(int key) {
        ListNode CurNode = this.head;
        while (CurNode != null) {
            if (CurNode.val == key) {
                return true;
            }
            CurNode = CurNode.next;
        }
        return false;
    }

    private ListNode findKeyPrevNode(int key) {
        ListNode CurNode = this.head;
        while (CurNode != null) {
            if (CurNode.next.val == key) {
                return CurNode;
            }
            CurNode = CurNode.next;
        }
        return null;
    }

    @Override
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
        if (this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        ListNode CurNode = findKeyPrevNode(key);
        if (CurNode == null) {
            System.out.println("没有要删除的数字");
            return;
        }
        ListNode DelNode = CurNode.next;
        CurNode.next = DelNode.next;
    }

    @Override
    public void removeAllKey(int key) {
        if (this.head == null) {
            return;
        }
        ListNode prev = this.head;
        ListNode cur = prev.next;
        while (cur != null) {
            if (cur.val == key) {
                cur = cur.next;
                prev.next = cur;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (this.head.val == key) {
            this.head = this.head.next;
        }

    }

    @Override
    public int size() {
        int count = 0;
        ListNode CurNode = this.head;
        while (CurNode != null) {
            count++;
            CurNode = CurNode.next;
        }
        return count;
    }

    @Override
    public void clear() {
        //法一：
        // this.head=null;

        //法二：
        ListNode CurNode = this.head;
        while (CurNode != null) {
            ListNode DelNode = CurNode.next;
            CurNode.next = null;
            CurNode = DelNode;
        }
    }

    @Override
    public void display() {
        ListNode CurNode = this.head;
        while (CurNode != null) {
            System.out.print(CurNode.val + " ");
            CurNode = CurNode.next;
        }
    }

    //逆置链表
    public ListNode reverseList() {
        if (head == null) {
            return null;
        }
            //只有一个节点
            if (head.next == null) {
                return head;
            }
                ListNode cur = head.next;
                head.next = null;
                while (cur!=null) {
                   ListNode curNext=cur.next;
                   cur.next=this.head;
                   this.head=cur;
                   cur=curNext;
                }
                return head;
            }
    public ListNode ERRORreverseList() {
        if (head == null) {
            return null;
        }
        //只有一个节点
        if (head.next == null) {
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur!=null) {
           ListNode reverseNode=cur;//反转的节点为cur
           reverseNode.next=this.head;//将反转的节点头插
           this.head=reverseNode;//更改头结点为新插入的节点
           cur=cur.next;//cur节点向后一个

        }
        return head;
    }

    public ListNode findMidNode() {
        int count=0;
        if(this.head==null) {
            return this.head;
        }
        ListNode cur=this.head;
        while (count!=size()/2) {
            count++;
            cur=cur.next;
        }
        return cur;
    }

    //快慢指针
    public ListNode MidNodeSlowFastpoint() {
        ListNode slowNode=this.head;
        ListNode fastNode=this.head;
        while (fastNode!=null&&slowNode!=null) {
            slowNode=slowNode.next;
            fastNode=fastNode.next.next;
        }
        return slowNode;
    }

    //倒数第k个节点
    //倒数第三
    public ListNode ReciprocelKNode(int k) {
        if (this.head==null) {
            return this.head;
        }
        ListNode slow=this.head;
        ListNode fast=this.head;
        for (int i = 0; i < k; i++) {
            fast=fast.next;
            if(fast==null) {
                return null;
            }
        }
        while (fast!=null&&slow!=null) {
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }

    //判断回文链表
    public boolean checkPalindrome() {
        if(head==null||head.next==null) {
            return true;
        }
        ListNode fast=this.head;
        ListNode slow=this.head;
        //找到中间位置
        while (fast!=null&&fast.next!=null) { //判断不能颠倒,不然会导致空指针异常
            fast=fast.next.next;
            slow=slow.next;
        }
        //翻转后半部分
        ListNode cur=slow.next;
        while(cur!=null) {
            ListNode curNext=cur.next;
            cur.next=slow;
            slow=cur;
            cur=curNext;
        }
        //从前到后,从后到前
        while(head!=slow) {
            if(head.val!=slow.val) {
                return false;
            }
            if(head.next==slow) {
                return true;     //考虑偶数情况,和上面的if不能颠倒顺序
            }
            head=head.next;
            slow=slow.next;

        }
        return true;
    }

}

